/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.dao;

import java.util.HashMap;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.colswe.solestac.entity.Request;

/**
 *
 * @author David Cendales
 */
public class RequestDao {
     /**Create a request.
     * @param r The request object that will be created
     * @param em EntityManager object to make the operation
     */
    public void create(final Request r, final EntityManager em) {
        em.persist(r);
    }
    
    /**Delete a request.
     * @param r The request object that will be deleted
     * @param em EntityManager object to make the operation
     */
    public void delete(final Request r, final EntityManager em) {
        em.remove(em.merge(r));
    }
    
    /**Update a request.
     * @param r The request object that will be updated
     * @param em EntityManager object to make the operation
     */
    public void update(final Request r, final EntityManager em) {
        em.merge(r);
    }
    
     /**Search requests based in the parameters given by the user.
     * @param em EntityManager object to make the operation
     * @param idRequest Id request given by user
     * @param idStudent Id student corresponding to identification given by the user
     * @param idType Id types corresponding to types request given by the user
     * @param idProgram Id program corresponding to program given by the user
     * @param idInstance Id instance corresponding to instance given by the user
     * @param academicPeriod Academic period given by the user
     * @return all requests by criteria filter
     */
    public List<Request> searchByFilter(final long idRequest, final long idStudent, final long[] idType,
            final long idProgram, final long idInstance, final String academicPeriod, final EntityManager em) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        String query = "SELECT r FROM Request r WHERE r.id >= 0 ";
        if (idRequest != 0) {
            params.put("idRequest", idRequest);
            query += "AND r.id = :idRequest ";
        }
        if (idStudent != 0) {
            params.put("idStudent", idStudent);
            query += "AND r.studentId.id = :idStudent ";
        }
        
        
        if (idType.length != 0) {
            params.put("idType0", idType[0]);
            query += "AND r.typeId.id = :idType0 ";
            for (int i = 1; i < idType.length; i++) {
                params.put("idType" + i, idType[i]);
                query += " OR r.typeId.id = :idType" + i + " ";
            }
        }
        if (idInstance != 0) {
            params.put("idInstance", idInstance);
            query += "AND r.instanceId.id = :idInstance ";
        }
        if (!academicPeriod.equals("")) {
            params.put("academicPeriod", academicPeriod);
            query += "AND r.academicPeriod = :academicPeriod ";
        }
        if (idProgram != 0) {
            params.put("idProgram", idProgram);
            query += "AND r.studentId.id = ANY(SELECT e.id FROM Student e WHERE e.programId.id = :idProgram) ";
        }
        query += "ORDER BY r.filingDate ";
        
        Query q = em.createQuery(query);
        for (String str : params.keySet()) {
            q.setParameter(str, params.get(str));
        }
        return (List<Request>) q.getResultList();
    }
    
}
